In [1]:
import os
import matplotlib.pyplot as plt
from matplotlib import style
style.use("fivethirtyeight")
from matplotlib.image import imread
import pandas as pd
import numpy as np
import seaborn as sns
In [2]:
datadir="C:\\Users\\personal\\Desktop\\Covid-19 x-Ray image\\corona"
In [3]:
os.listdir(datadir)
Out[3]:
['test', 'train']
In [4]:
train_path=datadir+"//train//"
test_path=datadir+"//test//"
In [5]:
os.listdir(train_path)
Out[5]:
['corona', 'normal']
In [6]:
corona_patients_trainingset=train_path+"corona"
normal_patients_trainingset=train_path+"normal"
corona_patients_testset=test_path+"corona"
normal_patients_testset=test_path+"normal"
In [7]:
os.listdir(corona_patients_trainingset)[0:5]
Out[7]:
['01E392EE-69F9-4E33-BFCE-E5C968654078.jpeg',
 '03BF7561-A9BA-4C3C-B8A0-D3E585F73F3C.jpeg',
 '1-s2.0-S0929664620300449-gr2_lrg-a.jpg',
 '1-s2.0-S0929664620300449-gr2_lrg-b.jpg',
 '1-s2.0-S0929664620300449-gr2_lrg-c.jpg']
In [8]:
os.listdir(normal_patients_trainingset)[0:5]
Out[8]:
['IM-0135-0001.jpeg',
 'IM-0137-0001.jpeg',
 'IM-0140-0001.jpeg',
 'IM-0141-0001.jpeg',
 'IM-0143-0001.jpeg']
In [9]:
corona_patient_xray_image=corona_patients_trainingset+"//01E392EE-69F9-4E33-BFCE-E5C968654078.jpeg"
corona_image=imread(corona_patient_xray_image)
corona_image
Out[9]:
array([[[182, 182, 182],
        [169, 169, 169],
        [152, 152, 152],
        ...,
        [254, 254, 254],
        [254, 254, 254],
        [254, 254, 254]],

       [[165, 165, 165],
        [150, 150, 150],
        [138, 138, 138],
        ...,
        [254, 254, 254],
        [254, 254, 254],
        [254, 254, 254]],

       [[146, 146, 146],
        [134, 134, 134],
        [128, 128, 128],
        ...,
        [254, 254, 254],
        [254, 254, 254],
        [254, 254, 254]],

       ...,

       [[243, 243, 243],
        [244, 244, 244],
        [245, 245, 245],
        ...,
        [254, 254, 254],
        [254, 254, 254],
        [254, 254, 254]],

       [[245, 245, 245],
        [245, 245, 245],
        [245, 245, 245],
        ...,
        [254, 254, 254],
        [254, 254, 254],
        [254, 254, 254]],

       [[254, 254, 254],
        [254, 254, 254],
        [254, 254, 254],
        ...,
        [254, 254, 254],
        [254, 254, 254],
        [254, 254, 254]]], dtype=uint8)
In [10]:
print(corona_image.shape[0])
print(corona_image.shape[1])
1482
1989
In [11]:
plt.figure(figsize=(10,6))
plt.imshow(corona_image)
plt.grid(False)
plt.show()
In [12]:
normal_patient_xray_image=normal_patients_trainingset+"//IM-0135-0001.jpeg"
normal_image=imread(normal_patient_xray_image)
In [13]:
plt.figure(figsize=(10,6))
plt.imshow(normal_image)
plt.grid(False)
plt.show()

corona patients

In [14]:
plt.figure(figsize=(19,12.5))
for i in range(25):
    plt.subplot(5,5,i+1)
    plt.grid(False)
    plt.gca().set_xticks([])
    plt.gca().set_xticks([])
    img=corona_patients_trainingset+"//"
    for j in os.listdir(corona_patients_trainingset)[i:i+1]:
        img=img+j
    plt.imshow(imread(img))
plt.show()    

normal patients

In [15]:
plt.figure(figsize=(19,12.5))
for i in range(25):
    plt.subplot(5,5,i+1)
    plt.grid(False)
    plt.gca().set_xticks([])
    plt.gca().set_xticks([])
    img=normal_patients_trainingset+"//"
    for j in os.listdir(normal_patients_trainingset)[i:i+1]:
        img=img+j
    plt.imshow(imread(img))
plt.show()    
In [16]:
print(len(os.listdir(corona_patients_trainingset)))
print(len(os.listdir(normal_patients_trainingset)))
130
134
In [17]:
print(len(os.listdir(corona_patients_testset)))
print(len(os.listdir(normal_patients_testset)))
10
10
In [18]:
cdim1=[]
cdim2=[]
l=len(os.listdir(corona_patients_trainingset))
for i in range(l):
    img=corona_patients_trainingset+"//"
    for j in os.listdir(corona_patients_trainingset)[i:i+1]:
        img=img+j
    cdim1.append(imread(img).shape[0])
    cdim2.append(imread(img).shape[1])
In [19]:
print(max(cdim1))
print(min(cdim1))
print(max(cdim2))
print(min(cdim2))
4095
237
4280
255
In [20]:
plt.figure(figsize=(10,6))
sns.jointplot(cdim1,cdim2)
Out[20]:
<seaborn.axisgrid.JointGrid at 0x1af9a0e0550>
<Figure size 720x432 with 0 Axes>
In [21]:
ndim1=[]
ndim2=[]
l=len(os.listdir(normal_patients_trainingset))
for i in range(l):
    img=normal_patients_trainingset+"//"
    for j in os.listdir(normal_patients_trainingset)[i:i+1]:
        img=img+j
    ndim1.append(imread(img).shape[0])
    ndim2.append(imread(img).shape[1])
In [22]:
print(max(ndim1))
print(min(ndim1))
print(max(ndim2))
print(min(ndim2))
2066
747
2396
994
In [23]:
plt.figure(figsize=(10,6))
sns.jointplot(ndim1,ndim2)
Out[23]:
<seaborn.axisgrid.JointGrid at 0x1af956ee3c8>
<Figure size 720x432 with 0 Axes>
In [24]:
print(np.mean(cdim1))
print(np.mean(cdim2))
print(np.mean(ndim1))
print(np.mean(ndim2))
1262.9076923076923
1384.7230769230769
1335.3432835820895
1672.1194029850747
In [25]:
dim1=int((np.mean(cdim1)+np.mean(ndim1))//2)
dim2=int((np.mean(cdim2)+np.mean(ndim2))//2)
shape=(dim1,dim2,3)
print(dim1,dim2)
imageshape=(500,500,3)
1299 1528

Image Augmentation

In [26]:
from tensorflow.keras.preprocessing.image import ImageDataGenerator
In [27]:
datagen=ImageDataGenerator(rotation_range=5,
                          width_shift_range=0.1,
                          height_shift_range=0.1,
                          shear_range=0.1,
                          zoom_range=0.1,
                          horizontal_flip=False,
                          vertical_flip=False,
                          fill_mode='nearest')
In [28]:
plt.figure(figsize=(10,6))
plt.imshow(corona_image)
plt.grid(False)
plt.show()
In [29]:
plt.figure(figsize=(10,6))
plt.imshow(datagen.random_transform(corona_image))
plt.grid(False)
plt.show()
In [30]:
datagen.flow_from_directory(train_path)
Found 264 images belonging to 2 classes.
Out[30]:
<keras_preprocessing.image.directory_iterator.DirectoryIterator at 0x1af99b10d30>
In [31]:
datagen.flow_from_directory(test_path)
Found 20 images belonging to 2 classes.
Out[31]:
<keras_preprocessing.image.directory_iterator.DirectoryIterator at 0x1af99bbd940>
In [32]:
from tensorflow.keras.layers import Dense,Conv2D,MaxPool2D,Flatten
from tensorflow.keras.models import Sequential
from tensorflow.keras.callbacks import EarlyStopping
In [33]:
def createmodel(optimizer="adam",loss="binary_crossentropy"):
    model=Sequential()
    model.add(Conv2D(filters=32,kernel_size=(3,3),input_shape=(500,500,3),activation="relu"))
    model.add(MaxPool2D(pool_size=(2,2)))
    model.add(Conv2D(filters=64,kernel_size=(3,3),input_shape=(500,500,3),activation="relu"))
    model.add(MaxPool2D(pool_size=(2,2)))
    model.add(Conv2D(filters=64,kernel_size=(3,3),input_shape=(500,500,3),activation="relu"))
    model.add(MaxPool2D(pool_size=(2,2)))
    model.add(Flatten())
    model.add(Dense(128,activation="relu"))
    model.add(Dense(1,activation="sigmoid"))
    model.compile(optimizer=optimizer,loss=loss,metrics=["accuracy"])
    return model
model=createmodel()
In [34]:
model.summary()
Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d (Conv2D)              (None, 498, 498, 32)      896       
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 249, 249, 32)      0         
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 247, 247, 64)      18496     
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 123, 123, 64)      0         
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 121, 121, 64)      36928     
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 60, 60, 64)        0         
_________________________________________________________________
flatten (Flatten)            (None, 230400)            0         
_________________________________________________________________
dense (Dense)                (None, 128)               29491328  
_________________________________________________________________
dense_1 (Dense)              (None, 1)                 129       
=================================================================
Total params: 29,547,777
Trainable params: 29,547,777
Non-trainable params: 0
_________________________________________________________________
In [35]:
callbacks=EarlyStopping(monitor="val_loss",mode="min",patience=2)
In [36]:
train_image_gen=datagen.flow_from_directory(train_path,
                                           target_size=imageshape[:2],
                                           color_mode='rgb',
                                           batch_size=12,
                                           class_mode='binary')
Found 264 images belonging to 2 classes.
In [37]:
test_image_gen=datagen.flow_from_directory(test_path,
                                           target_size=imageshape[:2],
                                           color_mode='rgb',
                                           batch_size=12,
                                           class_mode='binary',
                                          shuffle=False)
Found 20 images belonging to 2 classes.
In [38]:
results=model.fit_generator(train_image_gen,epochs=10,
                           validation_data=test_image_gen,
                           callbacks=[callbacks])
Epoch 1/10
22/22 [==============================] - 64s 3s/step - loss: 114.9939 - accuracy: 0.7538 - val_loss: 0.1010 - val_accuracy: 0.9500
Epoch 2/10
22/22 [==============================] - 64s 3s/step - loss: 0.9796 - accuracy: 0.7917 - val_loss: 0.0604 - val_accuracy: 1.0000
Epoch 3/10
22/22 [==============================] - 70s 3s/step - loss: 0.3736 - accuracy: 0.8902 - val_loss: 0.1228 - val_accuracy: 1.0000
Epoch 4/10
22/22 [==============================] - 66s 3s/step - loss: 0.2583 - accuracy: 0.9394 - val_loss: 0.0925 - val_accuracy: 0.9500
In [39]:
from tensorflow.keras.models import model_from_json
In [40]:
model_json=model.to_json()
with open("model.json","w") as json_file:
    json_file.write(model_json)
model.save_weights("model.h5")
In [41]:
json_file=open("model.json","r")
loaded_model_json=json_file.read()
json_file.close()
loaded_model=model_from_json(loaded_model_json)
loaded_model.load_weights("model.h5")
In [42]:
loaded_model.compile(loss="binary_crossentropy",optimizer="adam",metrics=["accuracy"])
In [43]:
loaded_model.evaluate_generator(test_image_gen)
Out[43]:
[0.13668369455263019, 0.95]
In [44]:
loaded_model.metrics_names
Out[44]:
['loss', 'accuracy']
In [45]:
loss=pd.DataFrame(model.history.history)
In [46]:
loss
Out[46]:
loss accuracy val_loss val_accuracy
0 114.993904 0.753788 0.101042 0.95
1 0.979554 0.791667 0.060416 1.00
2 0.373566 0.890152 0.122787 1.00
3 0.258323 0.939394 0.092534 0.95
In [47]:
plt.figure(figsize=(10,6))
loss[["loss","val_loss"]].plot()
Out[47]:
<matplotlib.axes._subplots.AxesSubplot at 0x1af9c80bf60>
<Figure size 720x432 with 0 Axes>
In [48]:
plt.figure(figsize=(10,6))
loss[["accuracy","val_accuracy"]].plot()
Out[48]:
<matplotlib.axes._subplots.AxesSubplot at 0x1af9c49d550>
<Figure size 720x432 with 0 Axes>
In [49]:
y_pred=loaded_model.predict_classes(test_image_gen)
In [50]:
y_predict_proba=loaded_model.predict_proba(test_image_gen)
In [51]:
from sklearn.metrics import confusion_matrix,classification_report,accuracy_score
In [52]:
print(confusion_matrix(test_image_gen.classes,y_pred))
print(classification_report(test_image_gen.classes,y_pred))
print(accuracy_score(test_image_gen.classes,y_pred)*100)
[[10  0]
 [ 0 10]]
              precision    recall  f1-score   support

           0       1.00      1.00      1.00        10
           1       1.00      1.00      1.00        10

    accuracy                           1.00        20
   macro avg       1.00      1.00      1.00        20
weighted avg       1.00      1.00      1.00        20

100.0
In [53]:
plt.figure(figsize=(10,6))
pl=sorted(y_predict_proba,reverse=False)
plt.plot(pl,marker='o',
         color='blue',markerfacecolor='red',
         markersize=5)
plt.show()
In [54]:
from tensorflow.keras.preprocessing import image
In [55]:
print(type(image.load_img(corona_patient_xray_image)))
image.load_img(corona_patient_xray_image)
<class 'PIL.JpegImagePlugin.JpegImageFile'>
Out[55]:
In [56]:
def imageprediction(imgpath):
    my_image=image.load_img(imgpath,target_size=imageshape)
    my_image=image.img_to_array(my_image)
    my_image=np.expand_dims(my_image,axis=0)
    class_type=loaded_model.predict_classes(my_image)
    class_prob=loaded_model.predict_proba(my_image)
    if class_type==[[0]]:
        n="Corona virus patient"
        acc=(1-class_prob)*100
        color="red"
    else:
        n="Normal patient"
        acc=class_prob*100
        color="blue"
    plt.imshow((image.load_img(imgpath)))
    plt.grid(False)
    plt.gca().set_xticks([])
    plt.gca().set_yticks([])
    plt.xlabel(n+str(acc)+" % accuracy",color=color)
    plt.show()
In [57]:
imageprediction(corona_patient_xray_image)
In [58]:
lct=len(os.listdir(corona_patients_testset))
for i in range(lct):
    img=corona_patients_testset+"//"
    for j in os.listdir(corona_patients_testset)[i:i+1]:
        img=img+j
    imageprediction(img)
In [59]:
lct=len(os.listdir(normal_patients_testset))
for i in range(lct):
    img=normal_patients_testset+"//"
    for j in os.listdir(normal_patients_testset)[i:i+1]:
        img=img+j
    imageprediction(img)